import { Bullet } from "./bullet.js";
import { Manager } from "./manager.js";
import { Role } from "./parentAir.js";

class Hero extends Role {
    constructor(w, h, x, y, imgUrl, boomUrl) {
        super(w, h, x, y, imgUrl, boomUrl)
        // this.collide();
        this.move();
        
        this.createBullet(100);
        
    }

    move() {
        let _this = this;
        this.item.mousedown((event) => {
            //禁止图片的默认拖动事件。
            event.preventDefault();
            $(window).bind('mousemove', (event) => {

                _this.x = event.clientX - _this.w / 2;
                _this.y = event.clientY - _this.h / 2;
                $(_this.item).css({
                    left: _this.x,
                    top: _this.y
                })
                //飞机爆炸
                this.collide()
            })
        })
        this.item.mouseup(() => {
            $(window).unbind('mousemove');
        })
    }

    createBullet(time) {
        let _this = this;
        this.bullet_itimer = setInterval(() => {
            new Bullet(6, 14, _this.x + _this.w / 2, _this.y - 14, ['./img/bullet1.png']);
        }, time);
    }
    collide(){
        let all = Manager.allEnemy;
        let [x, y, p_w, p_h] = [this.x, this.y, this.w, this.h];
        all.forEach((item) => {
            let [f_x, f_y, f_w, f_h] = [item.x, item.y, item.w, item.h];
            if (x + p_w > f_x && f_x + f_w > x && y + p_h > f_y
                && f_h + f_y > y) {
                this.boom()
                clearInterval(this.bullet_itimer);
                $('.gameOver').css('display','block')
                $('.gameOver div:nth-child(2)').html($('.sco').html())
            }
        })
    }
}
export { Hero }